#include "../include/parse_path.h"

int is_absolute_path(const char *path) {
    return path[0] == '/';  // 返回1表示是绝对路径
}

int parse_path(const char *current_path, const char *relative_path, char *path) {
    // 检查输入参数是否有效
    if (current_path == NULL || relative_path == NULL || path == NULL) {
        return -1;
    }

    size_t relative_path_length = strlen(relative_path);
    
    // 如果相对路径为空，直接使用当前路径
    if (relative_path_length == 0) {
        strncpy(path, current_path, PATH_MAX - 1);
        path[PATH_MAX - 1] = '\0';
        return 0;
    }
    
    // 检查路径长度是否超过限制
    if (relative_path_length >= PATH_MAX) {
        return -1;
    }
    
    // 如果是绝对路径，直接处理
    if (is_absolute_path(relative_path)) {
        dir_stack_t p;
        p.size = 0;
        
        // 处理绝对路径
        char abs_tmp[PATH_MAX];
        strncpy(abs_tmp, relative_path, PATH_MAX - 1);
        abs_tmp[PATH_MAX - 1] = '\0';
        
        char *token = strtok(abs_tmp, "/");
        while (token != NULL && p.size < MAX_STACK_COUNT) {
            if (strcmp(token, "..") == 0) {
                if (p.size > 0) {
                    p.size--;
                }
            } else if (strcmp(token, ".") != 0) {
                strncpy(p.path_stack[p.size], token, PATH_MAX - 1);
                p.path_stack[p.size][PATH_MAX - 1] = '\0';
                p.size++;
            }
            token = strtok(NULL, "/");
        }
        
        // 构建绝对路径
        path[0] = '/';
        path[1] = '\0';
        
        for (int i = 0; i < p.size; i++) {
            if (i > 0) {
                strncat(path, "/", PATH_MAX - strlen(path) - 1);
            }
            strncat(path, p.path_stack[i], PATH_MAX - strlen(path) - 1);
        }
        
        // 处理根目录情况
        if (strlen(path) == 0) {
            strncpy(path, "/", PATH_MAX - 1);
        }
        
        return 0;
    }
    
    // 处理相对路径
    dir_stack_t p;
    p.size = 0;
    
    // 先处理当前路径
    char cp_tmp[PATH_MAX];
    strncpy(cp_tmp, current_path, PATH_MAX - 1);
    cp_tmp[PATH_MAX - 1] = '\0';
    
    char *token = strtok(cp_tmp, "/");
    while (token != NULL && p.size < MAX_STACK_COUNT) {
        strncpy(p.path_stack[p.size], token, PATH_MAX - 1);
        p.path_stack[p.size][PATH_MAX - 1] = '\0';
        p.size++;
        token = strtok(NULL, "/");
    }
    
    // 处理相对路径部分
    char rp_tmp[PATH_MAX];
    strncpy(rp_tmp, relative_path, PATH_MAX - 1);
    rp_tmp[PATH_MAX - 1] = '\0';
    token = strtok(rp_tmp, "/");
    
    while (token != NULL && p.size < MAX_STACK_COUNT) {
        if (strcmp(token, "..") == 0) {
            if (p.size > 0) {
                p.size--;
            }
        } else if (strcmp(token, ".") != 0) {
            strncpy(p.path_stack[p.size], token, PATH_MAX - 1);
            p.path_stack[p.size][PATH_MAX - 1] = '\0';
            p.size++;
        }
        token = strtok(NULL, "/");
    }
    
    // 构建最终路径
    path[0] = '/';
    path[1] = '\0';
    
    for (int i = 0; i < p.size; i++) {
        if (i > 0) {
            strncat(path, "/", PATH_MAX - strlen(path) - 1);
        }
        strncat(path, p.path_stack[i], PATH_MAX - strlen(path) - 1);
    }
    
    // 处理根目录情况
    if (strlen(path) == 0) {
        strncpy(path, "/", PATH_MAX - 1);
    }
    
    return 0;
}

/*int main() {
    char relative_path[PATH_MAX];
    char current_path[PATH_MAX] = "/netdisk/client/src";
    char path[PATH_MAX];
    
    printf("请输入相对路径: ");
    if (fgets(relative_path, PATH_MAX, stdin) == NULL) {
        printf("输入错误\n");
        return 1;
    }
    
    // 去除换行符
    relative_path[strcspn(relative_path, "\n")] = '\0';
    
    if (parse_path(current_path, relative_path, path) == 0) {
        printf("最终路径: %s\n", path);
    } else {
        printf("路径解析失败\n");
    }
    
    return 0;
}*/

/*int is_absolute_path(const char *path) {
    return path[0] == '/';  // 返回1表示是绝对路径
}

int parse_path(const char *current_path, const char *relative_path, char *path) {
    size_t relative_path_length = strlen(relative_path);
    
    if (relative_path_length == 0) {
        strcpy(path, current_path);
        return 0;
    }
    
    if (relative_path_length >= PATH_MAX) {
        return -1;
    }
    
    // 如果是绝对路径，直接使用
    if (is_absolute_path(relative_path)) {
        if (relative_path_length + 1 > PATH_MAX) {
            return -1;
        }
        strcpy(path, relative_path);
        return 0;
    }
    
    // 处理相对路径
    stack_t p;
    p.size = 0;
    
    // 先将当前路径压入栈
    char cp_tmp[PATH_MAX];
    strcpy(cp_tmp, current_path);
    
    char *token = strtok(cp_tmp, "/");
    while (token != NULL) {
        strcpy(p.path_stack[p.size++], token);
        token = strtok(NULL, "/");
    }
    
    // 处理相对路径部分
    char rp_tmp[PATH_MAX];
    strcpy(rp_tmp, relative_path);
    token = strtok(rp_tmp, "/");
    
    while (token != NULL) {
        if (strcmp(token, "..") == 0) {
            if (p.size > 0) {
                p.size--;
            }
        } else if (strcmp(token, ".") != 0) {
            if (p.size >= MAX_STACK_COUNT) {
                return -1;
            }
            strcpy(p.path_stack[p.size++], token);
        }
        token = strtok(NULL, "/");
    }
    
    // 构建最终路径
    path[0] = '/';
    path[1] = '\0';
    
    for (int i = 0; i < p.size; i++) {
        if (i > 0) {
            strcat(path, "/");
        }
        strcat(path, p.path_stack[i]);
    }
    
    // 处理空路径情况
    if (strlen(path) == 0) {
        strcpy(path, "/");
    }
    
    return 0;
}

int main() {
    char relative_path[PATH_MAX];
    char current_path[PATH_MAX] = "/netdisk/client/include";
    char path[PATH_MAX];
    
    printf("请输入相对路径: ");
    scanf("%s", relative_path);
    
    if (parse_path(current_path, relative_path, path) == 0) {
        printf("最终路径: %s\n", path);
    } else {
        printf("路径解析失败\n");
    }
    
    return 0;
}*/



